//
//  PKRadiomanScou.m
//  PianKe
//
//  Created by mingtao on 15/10/4.
//  Copyright (c) 2015年 mingtao. All rights reserved.
//

#import "PKRadiomanScou.h"
#import <UIImageView+WebCache.h>
#import <UIButton+WebCache.h>

@interface PKRadiomanScou ()<UIScrollViewDelegate>

@end

@implementation PKRadiomanScou


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _scroView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIHES, HHIHEI/2-180)];

        _scroView.backgroundColor = [UIColor whiteColor];
        
        _scroView.contentSize = CGSizeMake(WIHES*3, HHIHEI/2-180);
        //每次翻动屏幕大小宽度
        _scroView.pagingEnabled = YES;
        
        //是否滚动
       // _scroView.scrollEnabled = YES;
        
        //隐藏滑动条
        
        _scroView.showsHorizontalScrollIndicator=NO;
        
        _scroView.delegate = self;
        
        for (int i =0; i<3; i++) {
            _imagepage = [[UIImageView alloc]initWithFrame:CGRectMake(WIHES*i,0 , WIHES, HHIHEI/2-180)];
            
          //  _imagepage.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
            
            [_scroView addSubview:_imagepage];
        }
        
        [self addSubview:_scroView];
        
        _page = [[UIPageControl alloc]initWithFrame:CGRectMake(275, 120, 100, 30)];
        

        _page.numberOfPages = 3;
        
        _page.currentPage = 0;
 
        
        [_page addTarget:self action:@selector(pageclick:) forControlEvents:UIControlEventValueChanged];
        
        [self addSubview:_page];
        
        
        _ImageViewNo1 = [UIButton buttonWithType:(UIButtonTypeSystem)];
        
        _ImageViewNo1.frame = CGRectMake(5, 155, WIHES/3-10, 130);
        
        _ImageViewNo1.backgroundColor = [UIColor yellowColor];
        
        [self addSubview:_ImageViewNo1];
        
        _ImageViewTo = [UIButton buttonWithType:(UIButtonTypeSystem)];
        _ImageViewTo.frame = CGRectMake(130, 155, WIHES/3-10, 130);
        
        _ImageViewTo.backgroundColor = [UIColor yellowColor];
        
        [self addSubview:_ImageViewTo];
        
        _ImageViewSrui = [UIButton buttonWithType:(UIButtonTypeSystem)];
        
        _ImageViewSrui.frame = CGRectMake(255, 155, WIHES/3-10, 130);
        
        _ImageViewSrui.backgroundColor = [UIColor yellowColor];
        
        [self addSubview:_ImageViewSrui];
        
        
    }
    
    return self;


}

-(void)setScrArray:(NSMutableArray *)ScrArray
{
    _ScrArray = ScrArray;
    
    for (int i =0; i<3; i++) {
        UIImageView *imageViews=_scroView.subviews[i];
        [imageViews sd_setImageWithURL:[NSURL URLWithString:[ScrArray[i] img]]];
        
    }
    [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    

}
-(void)nextImage
{
    if (_page.currentPage<2) {
        _page.currentPage++;
        [self pageclick:_page];
    }
    else if (_page.currentPage==2)
    {
        _page.currentPage=0;
        [self pageclick:_page];
    }
    
    
}


-(void)setArray:(NSMutableArray *)array
{
    _array = array;
    //[_butiamge setImage:[UIImage imageNamed:@"iconfont-touxiang"] forState:UIControlStateSelected];
    //    [_Imagecoverimg sd_setImageWithURL:[NSURL URLWithString:Radiomain.coverimg]];
    
    [_ImageViewNo1 sd_setBackgroundImageWithURL:[NSURL URLWithString:[array[0] coverimg]] forState:UIControlStateNormal];
    [_ImageViewTo sd_setBackgroundImageWithURL:[NSURL URLWithString:[array[1] coverimg]] forState:UIControlStateNormal];
    
    [_ImageViewSrui sd_setBackgroundImageWithURL:[NSURL URLWithString:[array[2] coverimg]] forState:UIControlStateNormal];

}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _page.currentPage = scrollView.contentOffset.x/scrollView.bounds.size.width;

}

-(void)pageclick:(UIPageControl *)page
{
    _scroView.contentOffset = CGPointMake(page.currentPage*self.bounds.size.width, 0);


}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
